package com.ruoyi.web.controller.app;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.HgtOperationDo;
import com.ruoyi.system.domain.HgtpieDo;
import com.ruoyi.system.domain.SysCfca;
import com.ruoyi.system.domain.node;
import com.ruoyi.system.service.HgtOperationService;
import com.ruoyi.system.service.ISysCfcaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;


@RestController
@RequestMapping("/qzcfca")
public class qzCfcaController {

    @Autowired
    private ISysCfcaService iSysCfcaService;

    @PostMapping ("getqznodeer")
    HashMap<Object, Object> getqznodeer( @RequestBody Map<String, Object> params) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();

        List<node> bb = iSysCfcaService.selectnode(params.get("nodename").toString());
        aa.put("code",0);
        aa.put("msg","提交成功");
        aa.put("nodeers",bb);
        map.put("data",aa);
        return map;
    }
    @PostMapping ("qzsave")
    HashMap<Object, Object> qzsave(@RequestBody SysCfca sysCfca) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();

        iSysCfcaService.insertSysCfca(sysCfca);
        aa.put("code",0);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping ("qzupdate")
    HashMap<Object, Object> qzupdate(@RequestBody SysCfca sysCfca) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = new Date();
        String currentTime = dateFormat.format(date1);
        if(sysCfca.getMyider().equals("yes")){
            if(sysCfca.getStates().equals("2")) {
                sysCfca.setStates("3");
                sysCfca.setNode1er(sysCfca.getLasteer());
                sysCfca.setNode1vice(sysCfca.getMyider());
                sysCfca.setTime1(currentTime);
            }
        } else if(sysCfca.getMyider().equals("Agree")){
            sysCfca.setStates("5");
            sysCfca.setNode2er(sysCfca.getLasteer());
            sysCfca.setNode2vice(sysCfca.getMyider());
            sysCfca.setTime2(currentTime);
        }else if(sysCfca.getMyider().equals("no")){
            sysCfca.setStates("1");
            sysCfca.setNodeer(sysCfca.getCeater());
        }
        iSysCfcaService.updateSysCfca(sysCfca);
        aa.put("code",0);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping ("findqzcfcaBynodeer")
    HashMap<Object, Object> findqzcfcaBynodeer(@RequestBody Map<String, Object> params) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        SysCfca sysCfca=new SysCfca();
        sysCfca.setNodeer(params.get("creatorname").toString());
        List<SysCfca> bb = iSysCfcaService.findqzcfcaBynodeer(sysCfca);
        aa.put("code",0);
        aa.put("msg","提交成功");
        aa.put("resultData",bb);
        map.put("data",aa);
        return map;
    }
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {

            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
}
